package com.ydh.company.service.impl;

import com.ydh.company.dto.ResponseData;
import com.ydh.company.dto.ResultData;
import com.ydh.company.mapper.PurchaseOrderMapper;
import com.ydh.company.mapper.StockMapper;
import com.ydh.company.mapper.WarehousingOutRecordMapper;
import com.ydh.company.pojo.PurchaseOrderDetail;
import com.ydh.company.pojo.PurchaseOrderMain;
import com.ydh.company.pojo.WarehousingOutRecordDetail;
import com.ydh.company.pojo.WarehousingOutRecordMain;
import com.ydh.company.service.PurchaseOrderService;
import com.ydh.company.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author YuYinYang
 * @date Creat at 2020/5/6
 * @description:
 **/
@Service
@SuppressWarnings("all")
public class PurchaseOrderServiceIpml implements PurchaseOrderService {
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;
    @Autowired
    private WarehousingOutRecordMapper warehousingOutRecordMapper;

    @Override
    public ResultData addPurchaseOrder(PurchaseOrderMain purchaseOrderMain) {
        int res1 =0;
        int res2 =0;
        //增加采购订单主表的数据
        purchaseOrderMain.setPurchaseOrderMainId(UUIDUtil.getId());
        res1=purchaseOrderMapper.addPurchaseOrderMain(purchaseOrderMain);
        //增加采购订单详情表的数据
        for (PurchaseOrderDetail pur:purchaseOrderMain.getPurchaseOrderDetail()
             ) {
            pur.setPurchaseOrderMainId(purchaseOrderMain.getPurchaseOrderMainId());
            pur.setPurchaseOrderDetailId(UUID.randomUUID().toString());
            res2=purchaseOrderMapper.addPurchaseOrderDetail(pur);
        }
        //判断操作是否成功，所有数据全部添加成功才为成功
        if (res1>0&res2>0){
            return ResponseData.success("成功增加");
        }
        return ResponseData.error(4001,"增加失败");
    }
//入库页面
    @Override
    public ResultData purchaseOrderWarehousingRecordPage(PurchaseOrderMain purchaseOrderMain) {
        Map<String,Object> map = new HashMap<>();
        if(purchaseOrderMain.getGoodsStatus()==0){
            //未入库时只需查询采购订单详情返回给前端页面
           PurchaseOrderMain purchaseOrderMain1 = purchaseOrderMapper.selectPurchaseOrderDetail(purchaseOrderMain.getPurchaseOrderNumber());
           map.put("PurchaseOrder",purchaseOrderMain1);
        }
        else if(purchaseOrderMain.getGoodsStatus()==1){
            //商品已入库则需将入库记录信息和采购订单信息一起返回给前端页面
            PurchaseOrderMain purchaseOrderMain1 = purchaseOrderMapper.selectPurchaseOrderDetail(purchaseOrderMain.getPurchaseOrderNumber());
            map.put("PurchaseOrder",purchaseOrderMain1);
            WarehousingOutRecordMain warehousingOutRecordMain =
                    warehousingOutRecordMapper.selectRecordByPurchaseOrderNumber(purchaseOrderMain.getPurchaseOrderNumber());
            map.put("WarehousingOutRecord",warehousingOutRecordMain);
        }
        return  ResponseData.success(map);
    }

    @Override
    public ResultData selectPurchaseOrderDetail(PurchaseOrderMain purchaseOrderMain) {
        return null;
    }

    @Override
    public ResultData selectPurchaseOrderList() {
        return null;
    }
    //入库操作
    @Override
    public ResultData purchaseOrderWarehousingById(WarehousingOutRecordMain warehousingOutRecordMain) {
         int res1 = 0;
         int res2 = 0;
         purchaseOrderMapper.updatePurchaseOrderWarehousingById(warehousingOutRecordMain.getPurchaseOrderMainId());
         warehousingOutRecordMain.setWarehousingRecordMainId(UUIDUtil.getId());
         res1=warehousingOutRecordMapper.addWarehousingRecordMain(warehousingOutRecordMain);
        for (WarehousingOutRecordDetail warehousing:warehousingOutRecordMain.getList()
             ) {
            warehousing.setWarehousingRecordDetailId(UUID.randomUUID().toString());
            warehousing.setWarehousingRecordMainId(warehousingOutRecordMain.getWarehousingRecordMainId());
            res2=warehousingOutRecordMapper.addWarehousingRecordDetail(warehousing);
        }

        if (res1>0&res2>0){
            return ResponseData.success("成功入库");
        }
        return ResponseData.error(4001,"入库失败");
    }

}
